<?php
ob_start();
//echo "t" . $_GET['t'];
session_start();
//echo "sess: " . $_SESSION['tid'];

include_once 'inc/global.php';

//if(strlen(session_id()==0))
//{
//  session_start();
//}



//if (!isset($_SESSION['tid']))
//    $_SESSION['tid'] = '';

if (isset($_SESSION['packageid']))
    $packageid = $_SESSION['packageid'];

$tid = $_GET['t'];// $_SESSION['tid'];
$tablename = $tables[$tid]['t'];
//echo "newtid :" . $tid;
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page - 1) * $rows;
$result = array();

include 'inc/conn.php';

if ($tid == 2) {
    //partners
//    $sql = "select count(*) from $tablename t1 inner join ats_city t2 
//                on t1.city=t2.id";
    $sql = "select count(*) from $tablename";
} elseif ($tid == 3) {
    //packages
    $sql = "select count(*) from $tablename";
} elseif ($tid == 6) {
    $sql = "select count(*) from $tablename where packageid=$packageid";
} elseif ($tid == 7) {
    $sql = "select count(*) from $tablename where packageid=$packageid";
} else {    //include tid "1"
    $sql = "select count(*) from $tablename";
}
$rs = mysql_query($sql);

$row = mysql_fetch_row($rs);
$result["total"] = $row[0];

if ($tid == 2) {
//    $sql = 'select t1.id, t1.partnername, t1.street1, t1.zipcode, t1.phone1, t1.email,t1.city, t2.city as cityname 
//            from ats_partners t1 inner join ats_city t2 
//                on t1.city=t2.id';
    
    $sql = 'select * from ats_partners';
    
    $sclause = '';
    if (isset($_GET['s']))
        $sclause = $_GET['s'];
        if ($sclause != '') {
        $whereclause = " where partnername like '%" . $sclause . "%' or street1 like '%".$sclause."%'";
        $sql .= $whereclause;
    }

    //$sql = "select * from $tablename limit $offset,$rows";
} elseif ($tid == 3) {
    //package
    $sql = "select 
            t1.*,t1.image as imagedisplay, case t1.isspecialoffer when 0 then 'No' else 'Yes' end as isspecialofferlabel,
            (select t2.country from ats_destination t2 where t2.id=t1.dest_id) as dest_country
            from ats_package t1";
    

    $sclause = '';
    if (isset($_GET['s']))
        $sclause = $_GET['s'];
        if ($sclause != '') {
        $whereclause = " where title like '%" . $sclause . "%' or short_title like '%".$sclause."%'";
        $sql .= $whereclause;
    }
} elseif ($tid == 5) {
    //contact us
    $sql = "select id,name,email,subject,message,
            case status
                when 0 then 'New'
                when 1 then 'Opened'
                else 'Replied'
            end as status
            from ats_contactus";
} elseif ($tid == 6) {
    $sql = "select * from ats_packageprices where packageid=$packageid";
} elseif ($tid == 7) {
    $sql = "select * from $tablename where packageid=$packageid";
} elseif ($tid == 9) {
    //partner contact
    $sql = "select id,name,email,domisili,subject,message,partnerid,partnermail
            from ats_partner_contact";
} else {
    $sql = "select * from $tablename";
}
$sql .= " limit $offset,$rows";

$rs = mysql_query($sql);

$items = array();
while ($row = mysql_fetch_object($rs)) {
    array_push($items, $row);
}
$result["rows"] = $items;

echo json_encode($result);
ob_end_flush();

?>